var leaderboardApp = angular.module("LeaderboardApp", []);

leaderboardApp.service("notifier", function() {
	this.notifySuccess = function(message, title, msg) {
		message.title = title; 
		message.text = msg;
		message.type = "alert-success";
		message.visible = true;
	};
	
	this.notifyError = function(message, title, msg) {
		message.title = title; 
		message.text = msg;
		message.type = "alert-danger";
		message.visible = true;
	};
	
	this.hideNotif = function(message) {
		message.visible = false;
	};
});

leaderboardApp.service("httpPost", function($http, notifier) {
	return {
		send: function(message, servlet, paramsJson, successFn) {
			$http({method: "POST", url: servlet, params: paramsJson, headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}})
				.success(function(data) {
					if(data.hasOwnProperty("code")) {
						if(data.code !== 0) {
							if(data.code === 1004 && window.location.href.indexOf("login.html") < 0) {
								window.location = "/Leaderboard/login.html";
							}
							notifier.notifyError(message, "Error", data.reason);
						}
					}
					successFn(data);
				});
		}
	};
});

function Paginator() {
	this.totalRows = 0;
	this.currentPage = 1;
	this.rowsPerPage = 10;
	this.pagesToShow = 5;
	this.filter = "";
	this.pageChanged = function() { };
	this.hasNext = function() { var pages = this.pages(); return this.currentPage < pages[pages.length-1]; };
	this.hasPrev = function() { return (this.currentPage > 1); };
	this.hasNextClass = function() { return (this.hasNext()? "": "disabled"); };
	this.hasPrevClass = function() { return (this.hasPrev()? "": "disabled"); };
	this.itemClass = function(item) { return (item == this.currentPage? "active": ""); };
	this.itemDesc = function(item) {
		if(item == this.currentPage) {
			return "Pagina " + item + " de un total de " + this.totalPages();
		} else {
			return item;
		}
	};
	this.next = function() { if(this.hasNext()) { this.currentPage++; this.pageChanged(); } };
	this.prev = function() { if(this.hasPrev()) { this.currentPage--; this.pageChanged(); } };
	this.first = function() { if(this.hasPrev()) { this.currentPage = 1; this.pageChanged(); } };
	this.last = function() { if(this.hasNext()) {this.currentPage = this.totalPages(); this.pageChanged(); } };
	this.toPage = function(page) { this.currentPage = page; this.pageChanged(); };
	this.totalPages = function() { return Math.ceil(this.totalRows / this.rowsPerPage); };
	this.description = function() { return "Total de registros: " + this.totalRows + ". Mostrando del " + (((this.currenPage-1) * this.rowsPerPage) + 1) + " al " + (this.currentPage * this.rowsPerPage) ; };
	this.updateTotal = function(total) { 
		this.totalRows = total;
	};
	this.pages = function() {
		var pgs = [];
		var start = (this.currentPage - Math.floor(this.pagesToShow / 2));
		if(start <= 0) {
			start = 1;
		}
		for(var i=start; i<=(Math.min(start+(this.pagesToShow), this.totalPages())); i++) {
			pgs.push(i);
		}
		if(pgs.length == 0) {
			pgs.push(1);
		}
		return pgs;
	};
}

leaderboardApp.directive('ngEnter', function () {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function (event) {
            if(event.which === 13) {
                scope.$apply(function (){
                    scope.$eval(attrs.ngEnter);
                });
 
                event.preventDefault();
            }
        });
    };
});